﻿using System;

namespace b5.简单工厂设计
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[3];     //arr数组用来储存类所需的变量

            //随机创建十个形状对象
            arr[0] = 1;
            ShapeFactory.createShape("Square", arr);
            arr[0] = 2; arr[1] = 2;
            ShapeFactory.createShape("Rectangle", arr);
            arr[0] = 3;
            ShapeFactory.createShape("Circle", arr);
            arr[0] = 3; arr[1] = 4; arr[2] = 5;
            ShapeFactory.createShape("Triangle", arr);
            arr[0] = 4;
            ShapeFactory.createShape("Square", arr);
            arr[0] = 5; arr[1] = 5;
            ShapeFactory.createShape("Rectangle", arr);
            arr[0] = 6;
            ShapeFactory.createShape("Circle", arr);
            arr[0] = 6; arr[1] = 8; arr[2] = 10;
            ShapeFactory.createShape("Triangle", arr);
            arr[0] = 7;
            ShapeFactory.createShape("Square", arr);
            arr[0] = 8; arr[1] = 8;
            ShapeFactory.createShape("Rectangle", arr);

            Console.WriteLine("总面积为：" + ShapeFactory.get_total_area());
            Console.ReadLine();
        }
    }
}

//构建接口
public interface shape
{
    double Cal_area();
}

//定义类表示四种形状：正方形、长方形、圆、三角形
public class Square : shape
{
    public double edge;

    public Square(double edge)
    {
        this.edge = edge;
    }

    public double Cal_area()
    {
        return edge * edge;
    }
}

public class Rectangle : shape
{
    public double Length;
    public double Width;

    public Rectangle(double Length, double Width)
    {
        this.Length = Length;
        this.Width = Width;
    }

    public double Cal_area()
    {
        return Length * Length;
    }
}

public class Circle : shape
{
    public double r;

    public Circle(double r)
    {
        this.r = r;
    }

    public double Cal_area()
    {
        return 3.14 * r * r;
    }
}

public class Triangle : shape
{
    public double L1;
    public double L2;
    public double L3;

    public Triangle(double L1, double L2, double L3)
    {
        this.L1 = L1;
        this.L2 = L2;
        this.L3 = L3;
    }

    public double Cal_area()
    {
        double p, S;
        p = (L1 + L2 + L3) / 2;
        S = p * (p - L1) * (p - L2) * (p - L3);
        S = Math.Sqrt(S);
        return S;
    }
}

//工厂设计
public class ShapeFactory
{
    public static double area_sum = 0;  //记录面积总和

    public static shape createShape(string Name, int[] arr)
    {
        shape shape = null;
        switch (Name)
        {
            case "Square":
                shape = new Square(arr[0]);
                area_sum += shape.Cal_area();
                return shape;
            case "Rectangle":
                shape = new Rectangle(arr[0], arr[1]);
                area_sum += shape.Cal_area();
                return shape;
            case "Circle":
                shape = new Circle(arr[0]);
                area_sum += shape.Cal_area();
                return shape;
            case "Triangle":
                shape = new Triangle(arr[0], arr[1], arr[2]);
                area_sum += shape.Cal_area();
                return shape;
            default:
                return null;
        }
    }

    public static double get_total_area()
    {
        return area_sum;
    }
}